package rjp.map;

import gnu.trove.iterator.TIntFloatIterator;
import gnu.trove.map.hash.TIntFloatHashMap;
import java.io.IOException;


/**
 *
 * @author mwh
 */
public class TMapSantaMap extends SantaMap {



    public TMapSantaMap(TIntFloatHashMap xs, TIntFloatHashMap ys,
            float maxX, float maxY, float minX, float minY) {
        super(xs, ys, maxX, maxY, minX, minY);

    }

    public TMapSantaMap(String fn) throws IOException {
        super(fn);
    }

   
    public TMapSantaMap[][] createSubMaps(int parts) {

        TMapSantaMap[][] maps = new TMapSantaMap[parts][parts];
        TIntFloatHashMap[][] XmapMaps = new TIntFloatHashMap[parts][parts];
        TIntFloatHashMap[][] YmapMaps = new TIntFloatHashMap[parts][parts];
        for (int i = 0; i < parts; i++) {
            for (int j = 0; j < parts; j++) {
                XmapMaps[i][j] = new TIntFloatHashMap(size() / parts);
                YmapMaps[i][j] = new TIntFloatHashMap(size() / parts);
            }
        }
        float[] maxxs = new float[parts];
        float[] maxys = new float[parts];
        float[] minxs = new float[parts];
        float[] minys = new float[parts];

        minxs[0] = minX;
        minys[0] = minY;
        maxxs[parts - 1] = maxX;
        maxys[parts - 1] = maxY;

        float partXSize = (maxX - minX) / ((float) parts);
        float partYSize = (maxY - minY) / ((float) parts);

        for (int i = 0; i < parts - 1; i++) {
            float xborder = minX + (i + 1) * partXSize;
            float yborder = minY + (i + 1) * partYSize;
            maxxs[i] = xborder;
            maxys[i] = yborder;
            minxs[i + 1] = xborder;
            minys[i + 1] = yborder;
        }



        for (TIntFloatIterator it = xs.iterator(); it.hasNext();) {
            it.advance();
            float x = it.value();
            float y = ys.get(it.key());
            int ypart = -1;
            for (int i = 0; i < parts && ypart < 0; i++) {
                if (y <= maxys[i]) {
                    ypart = i;
                }
            }
            int xpart = -1;
            for (int i = 0; i < parts && xpart < 0; i++) {
                if (x <= maxxs[i]) {
                    xpart = i;
                }
            }
            XmapMaps[xpart][ypart].put(it.key(), x);
            YmapMaps[xpart][ypart].put(it.key(), y);
        }

        for (int i = 0; i < parts; i++) {
            for (int j = 0; j < parts; j++) {
                maps[i][j] = new TMapSantaMap(XmapMaps[i][j], YmapMaps[i][j],
                        maxxs[i], maxys[j], minxs[i], minys[j]);
            }
        }
        return maps;
    }


    
}
